package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ab.util.AbImageUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.bean.ZhenxinhuaMsg;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

/**
 * Created by Administrator on 2015/4/9.
 */
public class ZhenxinhuaLiaotianAdapter extends BaseAdapter {

    Context context;
    List<ZhenxinhuaMsg> datas = new ArrayList<ZhenxinhuaMsg>();

    String daanDefult="点击回答问题……";




    public ZhenxinhuaLiaotianAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ZhenxinhuaMsg> datas) {
        this.datas = datas;
    }


    @Override
    public int getCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ZhenxinhuaMsg bean = datas.get(position);

        String wenti=bean.getWenti();
        String daan=bean.getDaan();


        int type = bean.getType();



        //发送的消息
        if (type == ZhenxinhuaMsg.SEND) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_zhenxinhua_left, null);
            TextView iconTv;
            TextView wentiTv;
            TextView daanTv;
             iconTv= (TextView) convertView.findViewById(R.id.icon);
             wentiTv= (TextView) convertView.findViewById(R.id.wentiTv);
            daanTv= (TextView) convertView.findViewById(R.id.daanTv);

            wentiTv.setText(wenti);
            daanTv.setText(daan);

            BitmapUtils utils = new BitmapUtils(context);

            String jicon=bean.getJicon();

            utils.display(iconTv,jicon);
        }
        //接受的消息
        else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_zhenxinhua_right, null);
            TextView iconTv= (TextView) convertView.findViewById(R.id.icon);
            TextView wentiTv= (TextView) convertView.findViewById(R.id.wentiTv);
            final TextView daanTv= (TextView) convertView.findViewById(R.id.daanTv);

            wentiTv.setText(wenti);
            daanTv.setText(daan);

            final TextView icon= (TextView) convertView.findViewById(R.id.icon);

//            icon.setBackgroundResource(R.drawable.male_one);

            MemberModel.getUSER(context, new MemberModel.GetUserLister() {
                @Override
                public void onSuccess(USER user) {
                    BmobFile micon = user.getIcon();
                    if (micon != null) {
                        String url = micon.getFileUrl(context);

                        ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {

                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });
                    } else {
//                    icon.setImageResource(R.drawable.ic_user_avatar_default);
//                        icon.setBackgroundResource(R.drawable.male_one);


                        int sexType = PreferencesUtils.getInt(context, "sexType");
                        Log.e("test", "用户头像：icon为空，性别为(0男1女)：" + sexType);
                        switch (sexType) {
                            case 0:
                                icon.setBackgroundResource(R.drawable.male_one);
                                break;
                            case 1:
                                icon.setBackgroundResource(R.drawable.female_one);
                                break;
                        }

                    }

                }

                @Override
                public void onFailure(int i, String s) {

                }
            });


//            BitmapUtils utils = new BitmapUtils(context);
//
//            String cicon=bean.getCicon();
//
//            if(cicon!=null&&!cicon.equals("")){
//                utils.display(iconTv,cicon);
//            }else{
//                iconTv.setBackgroundResource(R.drawable.male_one);
//            }






//            ImageLoader imageLoader = ImageLoader.getInstance();
//
//            imageLoader.displayImage(jicon,iconTv);
//
//
//            imageLoader.loadImage(hericon,new ImageLoadingListener() {
//                @Override
//                public void onLoadingStarted(String s, View view) {
//
//                }
//
//                @Override
//                public void onLoadingFailed(String s, View view, FailReason failReason) {
//
//                }
//
//                @Override
//                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                    icon.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
//
//                }
//
//                @Override
//                public void onLoadingCancelled(String s, View view) {
//
//                }
//            });

            daanTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(daanDefult.equals(daanTv.getText().toString())){
                        //调转到回答问题界面
                        if(mCallBackLister!=null){
                            mCallBackLister.onCallBack(position);
                        }
                    }

                }
            });
        }


        return convertView;
    }

    //自定义一个回调方法
    /**
     * 定义一个接口
     */
    public interface CallBackLister{
        public void onCallBack(int position);
    }

    /**
     * 初始化接口变量
     */
    public static CallBackLister mCallBackLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setCallBackLister(CallBackLister myCallBackLister)
    {
        mCallBackLister = myCallBackLister;
    }




}
